<?php
/**
 * 数据升级程序
 */

class UpdateController extends Common {

	public function __construct() {
        parent::__construct();
	}
	
	public function indexAction() {
		if (!$this->get('do')) {
			$model  = $this->model('model');
			$this->delDir($model->cache_dir); //清除数据缓存
			if (!file_exists($model->cache_dir)) @mkdir($model->cache_dir, 0777, true);
			$this->adminMsg('正在更新数据缓存，请不要关闭浏览器...', url('update/index', array('do'=>1, 'action'=>0)), 1, 1, 2);
		}
		$action	= (int)$this->get('action');
		$count  = 0;
		$version= $this->cache->get('version');
		switch ($action) {
			case 0: //1.7.3
				if ($version < 175) {
					$user   = $this->model('user');
					//升级管理表数据
					$data   = $user->where('salt=?', '')->select();
					if ($data) {
						foreach ($data as $t) {
							$salt = substr(md5(rand(0, 999)), 0, 10);
							$upd  = array(
								'salt'     => $salt,
								'password' => md5($t['password'] . $salt . $t['password']),
							);
							$user->update($upd, 'userid=' . $t['userid']);
							$count ++;
						}
					}
				}
				$this->adminMsg('管理员密码数据升级成功(#' . $count . ')...', url('update/index', array('do'=>1, 'action'=>$action+1)), 1, 1, 2);
			break;
			case 1: //1.7.3
				//升级会员表数据
				if ($version < 175) {
					$member = $this->model('member');
					$data   = $member->where('salt=?', '')->where('password<>?', '')->select();
					if ($data) {
						foreach ($data as $t) {
							$salt = substr(md5(rand(0, 999)), 0, 10);
							$upd  = array(
								'salt'     => $salt,
								'password' => md5($t['password'] . $salt . $t['password']),
							);
							$member->update($upd, 'id=' . $t['id']);
							$count ++;
						}
					}
				}
				$this->adminMsg('会员密码数据升级成功(#' . $count . ')...', url('update/index', array('do'=>1, 'action'=>$action+1)), 1, 1, 2);
			break;
			//1.7.5 生成网站配置、升级内容扩展数据、内容表数据、重命名各个模型中的表、删除内容模型及表单模型自增属性、从新生成表单内容id
			case 2: //1.7.5
				//生成网站配置
				if ($version < 175) {
					copy(CONFIG_DIR . 'config.ini.php', CONFIG_DIR . 'site' . DIRECTORY_SEPARATOR . '1.ini.php');
				}
				$this->adminMsg('多站点配置升级成功...', url('update/index', array('do'=>1, 'action'=>$action+1)), 1, 1, 2);
			break;
			case 3: //1.7.5
				//升级内容扩展数据
				if ($version < 175) {
					$content = $this->model('content');
					$fields	 = $content->get_table_fields();
					if (in_array('relation', $fields) && in_array('verify', $fields) && in_array('position', $fields)) {
						$data = $content->where('relation<>""')->orwhere('verify<>""')->orwhere('position<>""')->select();
						if ($data) {
							foreach ($data as $t) {
								$content->set_extend_data($t['id'], $t);
								$count ++;
							}
						}
					}
				}
				$this->adminMsg('内容扩展数据升级成功(#' . $count . ')...', url('update/index', array('do'=>1, 'action'=>$action+1)), 1, 1, 2);
			break;
			case 4: //1.7.5 重命名内容模型表
				//UPDATE `demo_model` SET `tablename`=replace(tablename, 'content_1_', 'content_')
				if ($version < 175) {
					$model	= $this->model('model');
					$data	= $model->where('typeid=1')->select();
					foreach ($data as $t) {
						if (strpos($t['tablename'], 'content_1_') === false) {
							$name	= str_replace('content_', 'content_1_', $t['tablename']);
							$model->update(array('tablename'=>$name), 'modelid='.$t['modelid']);
							//生成模型文件
							$model->create_model($name, $t['typeid']);
							$count ++;
							$model->query("RENAME TABLE `" . $this->content->prefix . $t['tablename'] . "` TO `" . $this->content->prefix . $name . "`");
						}
					}
				}
				$this->adminMsg('内容模型升级成功(#' . $count . ')...', url('update/index', array('do'=>1, 'action'=>$action+1)), 1, 1, 2);
			break;
			case 5: //1.7.5 生成内容模型主表
				if ($version < 175) {
					mysql_query("ALTER TABLE `{$this->content->prefix}content` DROP `relation` , DROP `verify` , DROP `position`");
					$this->content->query("DROP TABLE IF EXISTS `{$this->content->prefix}content_1`");
					$this->content->query("CREATE TABLE `{$this->content->prefix}content_1` (
					  `id` int(10) unsigned NOT NULL,
					  `catid` smallint(5) unsigned NOT NULL DEFAULT '0',
					  `modelid` smallint(5) NOT NULL,
					  `title` varchar(80) NOT NULL DEFAULT '',
					  `thumb` varchar(255) NOT NULL DEFAULT '',
					  `keywords` char(40) NOT NULL DEFAULT '',
					  `description` text NOT NULL,
					  `url` char(100) NOT NULL,
					  `listorder` tinyint(3) unsigned NOT NULL DEFAULT '0',
					  `status` tinyint(2) unsigned NOT NULL DEFAULT '1',
					  `hits` smallint(5) unsigned NOT NULL DEFAULT '0',
					  `sysadd` tinyint(1) NOT NULL COMMENT '是否后台添加',
					  `userid` smallint(8) NOT NULL,
					  `username` char(20) NOT NULL,
					  `inputtime` bigint(10) unsigned NOT NULL DEFAULT '0',
					  `updatetime` bigint(10) unsigned NOT NULL DEFAULT '0',
					  PRIMARY KEY `id` (`id`),
					  KEY `member` (`userid`,`catid`,`status`,`sysadd`,`updatetime`),
					  KEY `list` (`catid`,`status`,`updatetime`),
					  KEY `top` (`catid`,`status`,`hits`),
					  KEY `admin` (`listorder`,`catid`,`modelid`,`status`,`updatetime`)
					) ENGINE=MyISAM DEFAULT CHARSET=utf8");
					$this->content->query("INSERT INTO `{$this->content->prefix}content_1` SELECT * FROM `{$this->content->prefix}content`");
					//删除内容模型其他字段
					$fields	 = $this->content->get_table_fields();
					foreach ($fields as $f) {
						if ($f != 'id') {
							mysql_query("ALTER TABLE `{$this->content->prefix}content` DROP `" . $f . "`");
						}
					}
					mysql_query("ALTER TABLE `{$this->content->prefix}content` INDEX `status`");
					mysql_query("ALTER TABLE `{$this->content->prefix}content` INDEX `listorder`");
					mysql_query("ALTER TABLE `{$this->content->prefix}content` INDEX `catid`");
				}
				$this->adminMsg('内容主表升级成功...', url('update/index', array('do'=>1, 'action'=>$action+1)), 1, 1, 2);
			break;
			case 6: //1.7.5	表单模型处理 
				if ($version < 175) {
					$model	= $this->model('model');
					$data	= $model->where('typeid=3')->select();
					if ($data) {
						foreach ($data as $t) {
							if (strpos($t['tablename'], 'form_1_') === false) {
								$name	= str_replace('form_', 'form_1_', $t['tablename']);
								$model->update(array('tablename'=>$name), 'modelid='.$t['modelid']);
								//生成模型文件
								$model->create_model($name, $t['typeid']);
								$count ++;
								$model->query("RENAME TABLE `" . $this->content->prefix . $t['tablename'] . "` TO `" . $this->content->prefix . $name . "`");
								$model->query("ALTER TABLE `" . $this->content->prefix . $name . "` CHANGE `id` `id` INT( 10 ) NOT NULL");
							}
						}
					}
				}
				$this->adminMsg('表单模型升级成功(#' . $count . ')...', url('update/index', array('do'=>1, 'action'=>$action+1)), 1, 1, 2);
			break;
			case 7: //1.7.5	升级表单数据
				if ($version < 175) {
					$model	= $this->model('model');
					$data	= $model->where('typeid=3')->select();
					$mid	= (int)$this->get('mid');
					if ($data) {
						$form = $this->model('form');
						$row  = $form->select();
						if (empty($row)) $form->query("INSERT INTO `{$form->prefix}form` VALUES('10000')");
						foreach ($data as $i=>$t) {
							if (empty($mid)) $mid = $t['modelid'];
							if ($t['modelid'] == $mid) {
								$dform = $this->model($t['tablename']);
								$fdata = $dform->select();
								if ($fdata) {
									foreach ($fdata as $f) {
										$id = $form->get_form_id();
										$form->query("UPDATE `" . $form->prefix . $t['tablename'] . "` SET `id`=$id WHERE `id`=" . (int)$f['id']);
										$count ++;
									}
									$toid = $data[$i+1]['modelid'];
									if ($toid) {
										$this->adminMsg('升级表单【' . $t['modelname'] . '】数据成功(#' . $count . ')...', url('update/index', array('do'=>1, 'action'=>$action, 'mid'=>$toid)), 1, 1, 2);
									}
								}
							}
						}
					}
				}
				$this->adminMsg('升级表单数据成功...', url('update/index', array('do'=>1, 'action'=>$action+1)), 1, 1, 2);
			break;
			case 8: //1.7.5	审核状态升级
				if ($version < 175) {
					$this->content->update(array('status'=>1), '`status`=0');
					$this->content->update(array('status'=>1), '`status`=2');
					$this->content->update(array('status'=>0), '`status`=3');
				}
				$this->adminMsg('审核状态升级成功...', url('update/index', array('do'=>1, 'action'=>$action+1)), 1, 1, 2);
			break;
			default:
				@rename(__FILE__, __FILE__ . '_');
				$this->adminMsg('恭喜您FineCMS v' . CMS_VERSION . '升级完成<br>请更新全站缓存，再按F5刷新浏览器', '', 1, 1, 1);
			break;
		}
	}
	
}